﻿using RuoVea.ExEnum;
using RuoVea.Fams.Server.Service;
using RuoVea.OmiApi.SystemApp.Service.Dto;
using RuoVea.OmiApi.SystemApp.Service;

namespace RuoVea.Fams.Web.Controllers;

[Authorize(Roles = "None")]
public partial class AssetsController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly AssetsService _assetsService;
    /// <summary>
    /// 
    /// </summary>
    private readonly SysUserService _sysUserService;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="assetsService"></param> 
    public AssetsController(AssetsService assetsService,SysUserService sysUserService)
    {
        _assetsService = assetsService;
        _sysUserService = sysUserService;
        //RuoVea.OmiSystem.Controllers.UserController
    }

    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("assets/pages")]
    public async Task<RestfulResult<PageResult<FamsAssets>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception("未提供上传数据。请在Request.Body中携带上传数据。");
        AssetsPageParam? data = JsonConvert.DeserializeObject<AssetsPageParam>(dataStr);
        data ??= new AssetsPageParam { PageNo = 1, PageSize = 10 };

        var query = await _assetsService.GetPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 查询所有
    /// </summary>
    /// <returns></returns>
    [HttpGet("assets/list")]
    public async Task<RestfulResult<List<FamsAssets>>> GetlistAsync()
    {
        return await _assetsService.GetlistAsync();
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("assets/add")]
    public async Task<RestfulResult> AddAsync([FromBody] FamsAssets data)
    {
        return await _assetsService.AddAsync(data);
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("assets/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] FamsAssets data)
    {
        return await _assetsService.UpdateAsync(data);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("assets/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        return await _assetsService.DeleteAsync(data);
    }

    [HttpPost("assets/dispose")]
    public async Task<IActionResult> DisposeAsset([FromBody] DisposeRequest request)
    {
        try
        {
            var success = await _assetsService.SetDisposal(request.AssetId, request.DisposalDate, request.Reason);
            return Ok(new { Success = success });
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

    /// <summary>
    /// 获取用户列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("assets/getsysuserlist")]
    public async Task<RestfulResult<List<UserOutput>>> GetSysUserList()
    {
        RestfulResult<List<UserOutput>> restful = new RestfulResult<List<UserOutput>>
        {
            Code = CodeStatus.OK
        };
        try
        {
            RestfulResult<List<UserOutput>> restfulResult = restful;
            restfulResult.Data = await _sysUserService.UserList();
            return restful;
        }
        catch (Exception ex)
        {
            restful.Code = CodeStatus.BadRequest;
            restful.Message = ex.Message;
            return restful;
        }
    }
}
